import pagination from '@/components/pagination.vue'
import { Request, confirm, successToast, errorToast, getDate } from '@/utils'

let request = new Request()

export default {
	
	data () {
		return {
			filter : {
				searchType : '',
				status : '',
				date : []
			}, 
			
			pageQuery : {
				startDate : '',
				endDate : ''
			},
			status : {0 : '未支付', 1 : '支付成功', 2 : '支付失败'},
			orderList : [],
			
			downloadLoading : false
		}
	},
	
	components : {
		pagination
	},
	
	methods : {
		search : function(){
			let filter = Object.assign({}, this.filter)
			
			let date = filter.date
			
			if(date){
				filter.startDate = date[0]
				filter.endDate = date[1]
			}
			
			delete filter.date
			
			this.pageQuery = Object.assign({}, filter)
			
			this.$nextTick(() => {
				this.$refs.pagination.initPage()
			})

		},
		
		page : function(rows){
			this.deviceList.splice(0)
			rows.forEach(v => {
				this.deviceList.push(v)
			})
		},
		
		handleDownload() { //导出
			this.downloadLoading = true
			var param = Object.assign({},this.pageQuery)

			request.get("order/exportRechargeOrder",res=>{
				var list = []
				this.downloadLoading = false
				if(res.code == 0){
					try{
						var payTypeList = this.payTypeList
						res.data.forEach(v=>{
							var item = [v.orderNo, v.userMobile, v.rechargeTime, v.affiliationAccount, v.rechargeAmount, this.status[v.status]]
							list.push(item)
						})
						import('@/vendor/Export2Excel').then(excel => {
							try{
								const tHeader = ['订单号', '绑定账号手机号', '充值时间', '归属账号', '充值金额（元）', '状态']
		
								
								
								excel.export_json_to_excel({
									header: tHeader,
									data : list,
									filename: "充值订单汇总",
								})
							}catch(e){
								console.log(e.message)
							}
						}).catch(e => {
							console.log(e.message)
						})
					}catch(e){
						console.log(e.message)
					}
				}else{
					errorToast(res.msg, this)
				}
				
				
			},param)
			
		}
		
	},
	
	created () {
		let d1 = getDate(-7)
		let d2 = getDate()
		this.filter.date = [d1, d2]
		this.pageQuery.startDate = d1
		this.pageQuery.endDate = d2
		
		request.get('deviceManager/getAgentNames', res => {
			this.agentList = res.data
		})
	}
}